package com.itheima.health.service.impl;

import com.alibaba.fastjson.JSON;
import com.itheima.health.entity.Result;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;

import com.itheima.health.pojo.SccListDto;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Override
    public Result getAllSetmeal() {
        //应该返回Result<List<Setmeal>>集合信息
        //查询所有的套餐信息

        List<Setmeal> setmeals = setmealMapper.selectList(null);

        return new Result(true,"套餐信息返回成功",setmeals);
    }

    @Override
    public Result findById(Integer id) {
        //todo 根据套餐ID查询套餐详细信息（Result<Setmeal<checkGroups<>>
        // 包括套餐基本信息、套餐包含的检查组、检查组包含的检查项）
            //1.1查询套餐基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
            //1.2套餐包含的检查组
        List<CheckGroup>checkGroups = setmealMapper.findById(id);
            //1.3检查组包含的检查项
        String s = JSON.toJSONString(setmeal);
        SccListDto sccListDto = JSON.parseObject(s, SccListDto.class);
        sccListDto.setCheckGroups(checkGroups);
        sccListDto.setCheckGroups(checkGroups);
        for (CheckGroup checkGroup : checkGroups) {
            List<CheckItem> checkItems = checkGroupMapper.findByCheckGroupId(checkGroup.getId());
            checkGroup.setCheckItems(checkItems);
        }

        return new Result(true,"返回检查组成功",sccListDto);
    }
}
